package com.quan.web.controller.upms;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.common.annotation.RequiresPermissions;
import com.quan.common.base.pojo.JSONResult;
import com.quan.common.base.pojo.PageBean;
import com.quan.common.core.support.BaseController;
import com.quan.upms.rpc.api.UpmsAppService;
import com.quan.upms.rpc.model.UpmsApp;

@RestController
@RequestMapping(value = "/api/upms/app")
public class UpmsAppController extends BaseController {

	@Autowired
	private UpmsAppService upmsAppService;

	@RequiresPermissions(value="upms:app:add", description="添加应用")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody UpmsApp entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return JSONResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		if (this.upmsAppService.existValue(UpmsApp.class, "name", entity.getName()) > 0) {
			return JSONResult.failure("名称已存在");
		}
		if (this.upmsAppService.existValue(UpmsApp.class, "code", entity.getCode()) > 0) {
			return JSONResult.failure("编码已存在");
		}
		this.upmsAppService.insertSelective(entity);
		return JSONResult.ok();
	}

	@RequiresPermissions(value="upms:app:delete", description="删除应用")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.upmsAppService.deleteById(id);
		return JSONResult.ok();
	}
	
	@RequiresPermissions(value="upms:app:delete", description="删除应用")
	@GetMapping(value = "/deleteBatch")
	public Object deleteBatch(String ids) {
		this.upmsAppService.deleteByIds(ids) ;
		return JSONResult.ok();
	}

	@RequiresPermissions(value="upms:app:update", description="修改应用")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody UpmsApp entity, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			return JSONResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
		}
		
		UpmsApp oldApp = this.upmsAppService.selectOneById(entity.getId()) ;
		if(null != oldApp.getName() && !oldApp.getName().equals(entity.getName())) {
			if (this.upmsAppService.existValue(UpmsApp.class, "name", entity.getName()) > 0) {
				return JSONResult.failure("名称已存在");
			}
		}
		if(null != oldApp.getCode() && !oldApp.getCode().equals(entity.getCode())) {
			if (this.upmsAppService.existValue(UpmsApp.class, "code", entity.getCode()) > 0) {
				return JSONResult.failure("编码已存在");
			}
		}
		this.upmsAppService.updateSelective(entity);
		return JSONResult.ok();
	}

	@RequiresPermissions(value="upms:app:detail", description="查看应用详情")
	@GetMapping(value = "/get/{id}")
	public Object get(@PathVariable Integer id) {
		return this.upmsAppService.selectOneById(id);
	}

	@RequiresPermissions(value="upms:app:list", description="查询应用")
	@GetMapping(value = "/list")
	public Object list(UpmsApp entity) {
		return this.upmsAppService.selectPage(entity, new PageBean(this.request));
	}
}
